package com.noble.meether.mail.ui.stranger;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.bean.center.user.light.UserLightList;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.log.PToast;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.library.view.custom.state.CustomStatusListView;
import com.noble.library.widget.ListView.xlistview.ExListView;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 陌生人消息
 */
public class ChatStrangerAct extends BaseAppCompatActivity implements RequestComplete, ExListView.IXListViewListener, View.OnClickListener, PObserver {
    private CustomStatusListView customStatusListView;
    private ExListView exListView;
    private View foot;

    private ChatPageAdapter adapter;
    private List<UserLight> data = new ArrayList<>();

    private int page = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_friends);
        initTitle();
        initView();
        onRefresh();
    }

    private void initTitle() {
        setBackView();
        setTitle("聊过的人");
    }

    private void initView() {
        customStatusListView = findViewById(R.id.myfriend_list);
        exListView = customStatusListView.getExListView();
        foot = LayoutInflater.from(this).inflate(R.layout.vip_footer_tip, null);
        TextView btnVip = foot.findViewById(R.id.btn_vip);
        btnVip.setOnClickListener(this);
        btnVip.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        adapter = new ChatPageAdapter(this, data);
        exListView.setAdapter(adapter);
        customStatusListView.showLoading();
        MsgMgr.getInstance().attach(this);
    }

    @Override
    public void onRefresh() {
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        page = 1;
        ModuleMgr.getCommonMgr().reqHasChat(page, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getCommonMgr().reqHasChat(page, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.isOk()) {
            UserLightList infoList = (UserLightList) response.getBaseData();

            if (infoList.getLightList().size() != 0) {
                if (page == 1) {
                    if (data.size() != 0) {
                        exListView.removeFooterView(foot);
                        data.clear();
                    }
                }
                data.addAll(infoList.getLightList());
                if (data.size() < 10) {
                    showVipTips();
                }
                adapter.notifyDataSetChanged();
                customStatusListView.showExListView();
            } else {
                if (page == 1) {
                    customStatusListView.showNoData("还没有聊过的人");
                } else {
                    showVipTips();
                }
            }
            if (page == 1) {
                exListView.stopRefresh();
            } else {
                exListView.stopLoadMore();
            }
        } else {
            showVipTips();
            PToast.showShort(response.getMsg());
        }
    }

    private void showVipTips() {
        exListView.setPullLoadEnable(false);
        exListView.addFooterView(foot);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_vip:
                MeetUIShow.showMyVipAct(this);
                break;
        }
    }

    private boolean isRefresh = false;      // 是否刷新列表

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Update_Friend:
                try {
                    HashMap<String, Object> param = (HashMap<String, Object>) value;
                    isRefresh = ((int) param.get("status_friend")) == 1;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh) {
            isRefresh = true;
            onRefresh();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }
}